<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>编辑链接</title>
    {include file="public/resource/layui_css" /}
    <link rel="stylesheet" href="/static/admin/css/style.css">
</head>

<body>
    <!-- 内容主体区域 -->
    <div class="my-form">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>编辑链接</legend>
        </fieldset>
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block my-input">
                    <input type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入名称" class="layui-input my-text" value="{$detail.name}">
                    <input type="hidden" name="id" value="{$detail.id}">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">类型</label>
                <div class="layui-input-block my-min-input">
                    <select name="type" lay-verify="required" lay-search="">
                        <option value="1" {eq name="$detail.type" value="1"}selected="selected"{/eq}>友情链接</option>
                        <option value="2" {eq name="$detail.type" value="2"}selected="selected"{/eq}>底部链接</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">网址</label>
                <div class="layui-input-block my-input">
                    <input type="text" name="url" lay-verify="required" autocomplete="off" placeholder="请输入链接" class="layui-input my-text" value="{$detail.url}">
                    <input type="hidden" name="id" value="{$detail.id}">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">新窗口打开</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="target" {eq name="$detail.target" value="1"}checked{/eq} lay-skin="switch" lay-text="是|否">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="addLink">保 存</button>
                </div>
            </div>
        </form>
    </div>
    {include file="public/resource/layui_js" /}
    <script>
        layui.use(['form', 'upload', 'element', 'laydate', 'layer'], function () {
            let $ = layui.jquery, form = layui.form, layer = layui.layer;
            // 监听提交
            form.on('submit(addLink)', function (data) {
                data = data.field;
                if (data.target === 'on') {
                    data.target = 1;
                } else {
                    data.target = 0;
                }
                $.ajax({
                    type: 'post',
                    url: '/admin/link/save',
                    dataType: 'json',
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(data),
                    success: function (result) {
                        if (result.code === 0) {
                            layer.msg('编辑成功。');
                            setTimeout(function () {
                                location.href="/admin/link";
                            }, 1000);
                        } else {
                            layer.msg(result.msg);
                        }
                    }
                });
                return false;
            });
        });
    </script>
</body>

</html>